﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Collections;

namespace sqxt
{
    public partial class MedSite_wh : Form
    {
        public MedSite_wh()
        {
            InitializeComponent();
        }

        SqlConnection conn = new SqlConnection(Dbconnect.s1);

        private void MedSite_wh_Load(object sender, EventArgs e)
        {
            #region 读取居委
            ArrayList arr = new ArrayList();
            SqlCommand com = new SqlCommand("select MedSite_name from medsite ", conn);
            conn.Open();
            SqlDataReader sdr = com.ExecuteReader();
            while (sdr.Read())
            {

                arr.Add(sdr.GetString(0));
            }
            foreach (string s in arr)
            {
                comboBox1.Items.Add(s);
            }
            sdr.Close();
            conn.Close();
            #endregion
            textBox11.Visible = false;
        }

        private void comboBox1_TextChanged(object sender, EventArgs e)
        {
            if (!b)
            {
                conn.Open();
                string cx = "select * from medsite where MedSite_name='" + comboBox1.Text.Trim() + "'";
                SqlCommand sc = new SqlCommand(cx, conn);
                SqlDataReader sdr = sc.ExecuteReader();
                sdr.Read();
                {
                    textBox1.Text = sdr[0] is DBNull ? string.Empty : sdr.GetInt32(0).ToString();
                    textBox2.Text = sdr[2] is DBNull ? string.Empty : sdr.GetString(2);
                    textBox3.Text = sdr[3] is DBNull ? string.Empty : sdr.GetInt32(3).ToString();
                    textBox4.Text = sdr[4] is DBNull ? string.Empty : sdr.GetInt32(4).ToString();
                    textBox5.Text = sdr[5] is DBNull ? string.Empty : sdr.GetInt32(5).ToString();
                    textBox6.Text = sdr[6] is DBNull ? string.Empty : sdr.GetInt32(6).ToString();
                    textBox7.Text = sdr[7] is DBNull ? string.Empty : sdr.GetInt32(7).ToString();
                    textBox8.Text = sdr[8] is DBNull ? string.Empty : sdr.GetInt32(8).ToString();
                    textBox9.Text = sdr[9] is DBNull ? string.Empty : sdr.GetString(9);
                    textBox10.Text = sdr[10] is DBNull ? string.Empty : sdr.GetInt32(10).ToString();

                }

                textBox1.Enabled = true;
                textBox2.Enabled = true;
                textBox3.Enabled = true;
                textBox5.Enabled = true;
                textBox4.Enabled = true;
                textBox6.Enabled = true;
                textBox7.Enabled = true;
                textBox8.Enabled = true;
                textBox9.Enabled = true;
                textBox10.Enabled = true;
                button1.Enabled = true;

            }
        }
        bool b = false;
        private void button1_Click(object sender, EventArgs e)
        {
            if (b2)
            {
                if (textBox11.Text == "" || textBox1.Text == "")
                {
                    MessageBox.Show("居委名称和居委编码必须填写");
                }
                else
                {
                    SqlDataAdapter sda = new SqlDataAdapter("select * from medsite", Dbconnect.s1);
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    dt.PrimaryKey = new DataColumn[] { dt.Columns["MedSite_Id"] };
                    DataRow dr;
                    dr = dt.NewRow();
                    dr["MedSite_Id"] = textBox1.Text;
                    dr["MedSite_name"] = textBox11.Text;
                    dr["area"] = textBox2.Text;
                    dr["families"] = textBox3.Text;
                    dr["permanent_population"] = textBox4.Text;
                    dr["transient_population"] = textBox5.Text;
                    dr["man_population"] = textBox6.Text;
                    dr["woman_population"] = textBox7.Text;
                    dr["eld_population"] = textBox8.Text;
                    dr["natural_growth_rate"] = textBox9.Text;
                    dr["create_files"] = textBox10.Text;
                    dt.Rows.Add(dr);
                    SqlCommandBuilder scb = new SqlCommandBuilder(sda);
               


                    SqlDataAdapter sda1 = new SqlDataAdapter("select * from autonumber", Dbconnect.s1);
                    DataTable dt1 = new DataTable();
                    sda1.Fill(dt1);
                    dt1.PrimaryKey = new DataColumn[] { dt1.Columns["MedSite_code"] };
                    DataRow dr1;
                    dr1 = dt1.NewRow();
                   
                    dr1["Inhabitant_code"] = "1";
                    dr1["Lenght"] = "5";
                    dr1["MedSite_name"] = textBox11.Text;
                    dr1["MedSite_code"] = textBox1.Text.Trim();
                    dt1.Rows.Add(dr1);
                    SqlCommandBuilder scb1 = new SqlCommandBuilder(sda1);
                    sda1.Update(dt1);
                    sda.Update(dt);
                    MessageBox.Show("增加成功");
                }

            }
            else
            {
                b = true;
                SqlDataAdapter sda = new SqlDataAdapter("select * from medsite", Dbconnect.s1);
                DataTable dtxg = new DataTable();
                sda.Fill(dtxg);
                dtxg.PrimaryKey = new DataColumn[] { dtxg.Columns[0] };
                DataRow dr = dtxg.Rows.Find(textBox1.Text.Trim());
                dr[0] = textBox1.Text;
                dr[1] = comboBox1.Text;
                dr[2] = textBox2.Text;
                dr[3] = textBox3.Text;
                dr[4] = textBox4.Text;
                dr[5] = textBox5.Text;
                dr[6] = textBox6.Text;
                dr[7] = textBox7.Text;
                dr[8] = textBox8.Text;
                dr[9] = textBox9.Text;
                dr[10] = textBox10.Text;
                SqlCommandBuilder scb = new SqlCommandBuilder(sda);
                sda.Update(new DataRow[] { dr });
                MessageBox.Show("已成功修改");

            }
        }

        bool b2 = false;
        private void button2_Click(object sender, EventArgs e)
        {
            label12.Visible = true;
            textBox11.Visible = true;
            label1.Text = "居委名称";
            comboBox1.Visible = false;
            textBox1.Enabled = true;
            textBox2.Enabled = true;
            textBox3.Enabled = true;
            textBox5.Enabled = true;
            textBox4.Enabled = true;
            textBox6.Enabled = true;
            textBox7.Enabled = true;
            textBox8.Enabled = true;
            textBox9.Enabled = true;
            textBox10.Enabled = true;
            button1.Enabled = true;
            b2 = true;


        }
    }
}